package com.jhinwins.equipmenteb.equipmentebserver.utils;

import com.alibaba.fastjson.JSONObject;

/**
 * 注意   obj需为pojo或pojo的集合形式
 * msg和info的区别:  info为状态码解释，无需自己写，msg为可携带参数，可作为参数传入
 */
public class RespUtils {
    private String respDesc;
    private Integer respCode;

    public static final RespUtils SUCCESS = new RespUtils("success", 100);
    public static final RespUtils UN_LOGIN = new RespUtils("unlogin", 200);


    private RespUtils() {
    }

    private RespUtils(String respDesc, Integer respCode) {
        this.respDesc = respDesc;
        this.respCode = respCode;
    }

    public static JSONObject createSucResp() {
        return createResp(RespUtils.SUCCESS);
    }

    public static JSONObject createSucResp(Object o) {
        return createResp(RespUtils.SUCCESS, o);
    }

    public static JSONObject createSucResp(String msg) {
        return createResp(RespUtils.SUCCESS, msg);
    }

    public static JSONObject createSucResp(String msg, Object o) {
        return createResp(RespUtils.SUCCESS, msg, o);
    }

    public static JSONObject createResp(RespUtils status) {
        return createResp(status, null);
    }

    public static JSONObject createResp(RespUtils status, Object o) {
        return createResp(status, null, o);
    }

    public static JSONObject createResp(RespUtils status, String msg) {
        return createResp(status, msg, null);
    }

    /**
     * 创建一个基本的响应
     */
    public static JSONObject createResp(RespUtils status, String msg, Object data) {

        JSONObject resp = new JSONObject();
        resp.put("code", status.respCode);
        resp.put("info", status.respDesc);
        if (msg != null) {
            resp.put("msg", msg);
        }
        if (data != null) {
            resp.put("data", data);
        }
        return resp;
    }

}
